package com.Servlet;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if ("admin".equals(username)&&"123".equals(password)){
            //通过请求域共享数据
            req.setAttribute("username",username);
            //处理登录成功的请求 -- 使用请求转发可以实现多个Servlet之间的数据共享
            req.getRequestDispatcher("/success").forward(req,resp);
        }else {
            req.setAttribute("loginMsg","账户或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);

        }
    }
}
